package cn.iocoder.yudao.module.bpm.controller.admin.formdata;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.controller.admin.formdata.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.formdata.FormDataDO;
import cn.iocoder.yudao.module.bpm.convert.formdata.FormDataConvert;
import cn.iocoder.yudao.module.bpm.service.formdata.FormDataService;

@Api(tags = "a管理后台 - 表单详情")
@RestController
@RequestMapping("/bpm/form-data")
@Validated
public class FormDataController {

    @Resource
    private FormDataService formDataService;

    @PostMapping("/create")
    @ApiOperation("创建表单详情")
    @PreAuthorize("@ss.hasPermission('bpm:form-data:create')")
    public CommonResult<Long> createFormData(@Valid @RequestBody FormDataCreateReqVO createReqVO) {
        return success(formDataService.createFormData(createReqVO));
    }

    @PostMapping("/createMuti")
    @ApiOperation("创建多条表单详情")
    public CommonResult<Long> createMutiFormData(@RequestBody List<FormDataCreateReqVO> createReqVO) {
        return success(formDataService.createMutiFormData(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新表单详情")
    @PreAuthorize("@ss.hasPermission('bpm:form-data:update')")
    public CommonResult<Boolean> updateFormData(@Valid @RequestBody FormDataUpdateReqVO updateReqVO) {
        formDataService.updateFormData(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateMuti")
    @ApiOperation("更新多条表单详情")
    @PreAuthorize("@ss.hasPermission('bpm:form-data:update')")
    public CommonResult<Boolean> updateMutiFormData(@RequestBody List<FormDataUpdateReqVO> updateReqVO) {
        formDataService.updateMutiFormData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除表单详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:form-data:delete')")
    public CommonResult<Boolean> deleteFormData(@RequestParam("id") Long id) {
        formDataService.deleteFormData(id);
        return success(true);
    }

    @DeleteMapping("/deleteByFormdef")
    @ApiOperation("通过表单标识删除表单详情")
    @PreAuthorize("@ss.hasPermission('bpm:form-data:delete')")
    public CommonResult<Boolean> deleteBfdFormData(@RequestParam("formDef") String formDef) {
        formDataService.deleteBfdFormData(formDef);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得表单详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:form-data:query')")
    public CommonResult<FormDataRespVO> getFormData(@RequestParam("id") Long id) {
        FormDataDO formData = formDataService.getFormData(id);
        return success(FormDataConvert.INSTANCE.convert(formData));
    }

    @GetMapping("/list")
    @ApiOperation("获得表单详情列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('bpm:form-data:query')")
    public CommonResult<List<FormDataRespVO>> getFormDataList(@RequestParam("ids") Collection<Long> ids) {
        List<FormDataDO> list = formDataService.getFormDataList(ids);
        return success(FormDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/listByFormDef")
    @ApiOperation("根据表单标识获得表单详情列表")
    @PreAuthorize("@ss.hasPermission('bpm:form-data:query')")
    public CommonResult<List<FormDataRespVO>> getFormDataBfdList(FormDataExportReqVO exportReqVO) {
        List<FormDataDO> list = formDataService.getFormDataBfdList(exportReqVO);
        return success(FormDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得表单详情分页")
    @PreAuthorize("@ss.hasPermission('bpm:form-data:query')")
    public CommonResult<PageResult<FormDataRespVO>> getFormDataPage(@Valid FormDataPageReqVO pageVO) {
        PageResult<FormDataDO> pageResult = formDataService.getFormDataPage(pageVO);
        return success(FormDataConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出表单详情 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:form-data:export')")
    @OperateLog(type = EXPORT)
    public void exportFormDataExcel(@Valid FormDataExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FormDataDO> list = formDataService.getFormDataList(exportReqVO);
        // 导出 Excel
        List<FormDataExcelVO> datas = FormDataConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "表单详情.xls", "数据", FormDataExcelVO.class, datas);
    }

}
